<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use View;
use App\Model\Banner;

class BannerController extends AdminBaseController
{
    public function __construct()
    {
        $option = ['module_name'=>'banner','status'=>'banner_status'];
        $this->setModelObj(new Banner,$option);
    }

	/**
	 * 后台Banner列表
	 */
	public function listData(Request $request,$classify = null)
	{
		if($classify){
			$banner_list = Banner::where('banner_classify',$classify)->get();
		}else{
			$banner_list = Banner::get();
		}
		$classifylist = Banner::distinct()->pluck('banner_classify');
		return View::make('admin.banner.bannerlist', ['bannerlist'=>$banner_list,'classifylist'=>$classifylist, 'classify'=>$classify]);
	}
	
	/**
	 * 后台Banner添加保存
	 */
	public function toAddData(Request $request)
	{
		$data = $request->except(['_token','_method']);
		$data['link_url'] = implode('::::::',$data['link_url']);
		//Base64 保存图片
		if(isset($data['banner_url'])){
			$fileroot = 'uploads/images/'.date("Ymd").'/';
			$data['banner_url'] = $this->basePic($data['banner_url'],$fileroot);
		}
		$re = Banner::insert($data);
        $jsonData = $re ? $this->getJsonData(2,'增加成功') : $this->getJsonData(0,'增加失败');
		return response()->json($jsonData);
	}
	
	/**
	 * 后台Banner编辑保存
	 */
	public function toUpdateData(Request $request, $id)
	{
		$data = $request->except(['_token','_method']);
		$data['link_url'] = implode('::::::',$data['link_url']);
		//Base64 保存图片
		if(isset($data['banner_url'])){
			$fileroot = 'uploads/images/'.date("Ymd").'/';
			$data['banner_url'] = $this->basePic($data['banner_url'],$fileroot);
		}
		$re = Banner::where('banner_id',$id)->update($data);
        $jsonData = $re ? $this->getJsonData(2,'编辑成功') : $this->getJsonData(0,'编辑失败');
		return response()->json($jsonData);
	}
}